package com.gitee.fanx.notes.service.impl;

import com.gitee.fanx.notes.entity.Notes;
import com.gitee.fanx.notes.mapper.*;
import com.gitee.fanx.notes.service.NotesService;
import com.gitee.fanx.notes.common.Asserts;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author fanx
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class NotesServiceImpl implements NotesService {

	private Logger log = LoggerFactory.getLogger(NotesServiceImpl.class);

	@Autowired
	private NotesMapper notesMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(Notes notes) {
		Asserts.notNull(notes, "提交内容不能为空");
		Asserts.notNull(notes.getName(), "姓名不能为空");
		Asserts.notNull(notes.getContent(), "留言内容不能为空");
		notes.setCreateTime(new Date());
		log.info("新增留言: {}", notes);
		int flag = this.notesMapper.insert(notes);
		Asserts.state(flag == 1, "保存失败");
		log.info("留言已保存: {}", notes);
	}

	@Override
	public List<Notes> getAll() {
		return notesMapper.getAll();
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteById(Long id) {
		Asserts.notNull(id, "信息ID不能为空");
		log.info("删除留言: {}", id);
		int flag = this.notesMapper.deleteById(id);
		Asserts.state(flag == 1, "删除失败");
		log.info("留言 {} 已删除", id);
	}

}
